package com.sanqi.common.utils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import com.sanqi.common.annotation.SanqiSimpleExcel;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;

public class SqSimpleExcelUtil {
    public static <T> List<T> excelBeanList(Sheet sheet, Class<T> clazz) {
        return excelBeanList(sheet, clazz, 0);
    }

    public static <T> List<T> excelBeanList(Sheet sheet, Class<T> clazz, Integer headerIndex) {
        Map<Integer, Field> headerFieldMap = MapUtil.newHashMap();// 标题Map
        Map<String, Field> beanMap = MapUtil.newHashMap();//
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            SanqiSimpleExcel annotation = field.getAnnotation(SanqiSimpleExcel.class);
            if (null != annotation) {
                String title = annotation.title();
                beanMap.put(title, field);
            }
        }

        List<T> resultList = ListUtil.list(false);
        // 获取标题
        Row headerRow = sheet.getRow(headerIndex);
        Iterator<Cell> cellIterator = headerRow.cellIterator();
        while (cellIterator.hasNext()) {
            Cell cell = cellIterator.next();
            String cellValue = PoiExcelUtil.getCellValue(cell);
            Field field = beanMap.get(StrUtil.trim(cellValue));
            if (field != null) {
                int rowIndex = cell.getColumnIndex();
                headerFieldMap.put(Integer.valueOf(rowIndex), field);
            }
        }
        for (int i = headerIndex + 1; i <= sheet.getLastRowNum(); i++) {
            Row row = sheet.getRow(i);

            try {
                T t = clazz.getDeclaredConstructor().newInstance();
                for (Iterator<Cell> iterator = row.cellIterator(); iterator.hasNext();) {
                    Cell cell = iterator.next();
                    int columnIndex = cell.getColumnIndex();
                    Field field = headerFieldMap.get(columnIndex);
                    if(field == null) {
                        continue;
                    }
                    Class<?> type = field.getType();
                    String cellValue = PoiExcelUtil.getCellValue(cell);
                    CellType cellType = null;
                    if(cell != null) {
                        cellType = cell.getCellType();
                    }
                    if(type.isPrimitive()) {
                        
                    } else {
                        if (type == String.class) {
                            field.setAccessible(true);
                            field.set(t, cellValue);
                            field.setAccessible(false);
                        }
                        if (type == Date.class) {
                            if(null != cell && StrUtil.isNotBlank(cellValue)) {
                                Date dateCellValue = cell.getDateCellValue();
                                field.setAccessible(true);
                                field.set(t, dateCellValue);
                                field.setAccessible(false);
                            }
                        }
                    }
                }

                resultList.add(t);
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (SecurityException e) {
                e.printStackTrace();
            }
        }
        return resultList;
    }

    public static <T> List<T> excelBeanList(Workbook workBook, String sheetName, Class<T> clazz) {
        Sheet sheet = workBook.getSheet(sheetName);
        return excelBeanList(sheet, clazz);
    }
    
    public static <T> List<T> excelBeanList(Workbook workBook, String sheetName, Class<T> clazz, Integer headerIndex) {
        Sheet sheet = workBook.getSheet(sheetName);
        return excelBeanList(sheet, clazz, headerIndex);
    }
}
